 <%   
 /** 
 * Description : select on of Ten welcome massages randomly using DBMS
 * update : logging error message when Database is unavailable using log4j
 * Date : 2/22/2014, update date : 2/28/2014
 * Written by : Kapsoo kim(nagoorum@naver.com) 
 */ 
 %>


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.DefectTracking.*" %>
<%@ page import="java.util.*, java.text.*" %>



<%

	/////////////////////////////////////
	// Update Database function /////
	/////////////////////////////////////

	
	//Receive data from edit.jsp page(defect_id, defect_update, defect_status, assignee_id)
	int defect_id = Integer.parseInt(request.getParameter("defect_id"));
	String summary = request.getParameter("summary");
	String defect_details_update = request.getParameter("defect_details_update").replaceAll("\"", "'");
	int defect_status = Integer.parseInt(request.getParameter("defect_status"));
	int assignee_id =Integer.parseInt(request.getParameter("assignee_id")); //in case unassigne
	String email_add = request.getParameter("email_add");
	
	String sql_defect_update = null;
	int update_result = 0 ; 
	
	//TEMP--just verify the value of the webpage
	out.println("<br><br>defect_id=" + defect_id );
	out.println("<br><br>defect_update=" + defect_details_update );
	out.println("<br><br>defect_status=" + defect_status );
	out.println("<br><br>assignee_id=" + assignee_id );
	out.println("<br><br>email_add=" + email_add );
	
	//Determine update sql statement  
	if (defect_status == 5) { //if defect's status is 5(closed), need to set closeddate 
		   	java.util.Date to_date = new java.util.Date(); 
		    SimpleDateFormat sd_f = new SimpleDateFormat("yyyy-MM-dd");
		    String close_date = sd_f.format(to_date).toString();
		    if(assignee_id == 0){//in case : do not assignee if assignee is unaviable
					sql_defect_update = "update defect SET `Details`=\"" + defect_details_update +"\", `StatusID`='" + defect_status +
		    			"',`ClosedDate`='" + close_date + "' WHERE `DefectID`='" + defect_id +"'";	
		    }else{
					sql_defect_update = "update defect SET `Details`=\"" + defect_details_update +"\", `StatusID`='" + defect_status + "',`AssignedTO`='" +
			    		assignee_id +  "',`ClosedDate`='" + close_date + "' WHERE `DefectID`='" + defect_id +"'";	
		   	}
	}else{
		    if(assignee_id == 0){ //in case : do not assignee if assignee is unaviable
					sql_defect_update = "update defect SET `Details`=\"" + defect_details_update +"\", `StatusID`='" + defect_status + 
						 "' WHERE `DefectID`='" + defect_id +"'";
		    }else{
					sql_defect_update = "update defect SET `Details`=\"" + defect_details_update +"\", `StatusID`='" + defect_status + "',`AssignedTO`='" +
						assignee_id + "' WHERE `DefectID`='" + defect_id +"'";		    
		    }
	}//if-else
	
	//Declear DefectDetailManger 
	DefectDetailManager dmgr = new DefectDetailManager();
	
	update_result = dmgr.setDefectDetailUpdate(sql_defect_update);
	
	//TEMP-just verify the process
	out.println("<br><br>sql=" + sql_defect_update );
	if (update_result > 0) {
			out.println("<br>Successfuly updated<br><br>");
	}else{
			out.println("<br>Failed update<br><br>");
	}
	
	
	
	/////////////////////////////////////
	// Mail sending function /////
	/////////////////////////////////////
	
	//TEMP-mail address to test
	email_add = "nagoorum@naver.com";
	
	
	//Declear SendMail
	SendMail sendmail = new SendMail();
	sendmail.SendMainToAssignee(email_add, summary, defect_details_update);
	
	
	//to list.jsp pages
	response.sendRedirect("./list.jsp");
	
%>
<br>